package applet.attendee.unlocked.components.summary;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;

import applet.attendee.CMSAttendeeView;
import applet.attendee.unlocked.components.checkIn.CMSCheckInDialog;
import applet.misc.action.EnableActionListener;
import applet.misc.document.EnableDocumentListener;
import applet.misc.graphics.AttendeeBackgroundPanel;

public class CMSAttendeeSummaryView extends AttendeeBackgroundPanel
{
	private static final long serialVersionUID = 6490614358660371039L;
	
	private CMSAttendeeView attendeeView;
	public CMSAttendeeView attendeeView() { return attendeeView; }
	
	private JTextField fNameField;
	private JTextField lNameField;
	private JTextField orgField;
	private JButton saveButton;
	private JButton checkInButton;

	public CMSAttendeeSummaryView(CMSAttendeeView attendeeView)
	{
		super(new GridBagLayout());
		this.attendeeView = attendeeView;
		setBorder(new EmptyBorder(25, 25, 25, 25));
		
		defineComponents();
		
		JPanel summaryDialog = new JPanel(new BorderLayout());
			summaryDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
			
				JPanel titlePane = new JPanel(new BorderLayout());
				titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
				titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
					JLabel title = new JLabel("Attendee Data");
					title.setFont(title.getFont().deriveFont(Font.BOLD));
					titlePane.add(title, BorderLayout.WEST);
				summaryDialog.add(titlePane, BorderLayout.NORTH);
				
			summaryDialog.add(getPersonalInfoPanel(), BorderLayout.CENTER);
			summaryDialog.add(getCheckInPanel(), BorderLayout.SOUTH);
		add(summaryDialog);
	}
	
	private void defineComponents()
	{
		saveButton = new JButton("Save");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.addActionListener(new UpdateInfoListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));
		saveButton.setEnabled(false);
		
		fNameField = new JTextField(15);
		fNameField.setText(attendeeView.attendee().firstName());
		fNameField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		lNameField = new JTextField(15);
		lNameField.setText(attendeeView.attendee().lastName());
		lNameField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		orgField = new JTextField(15);
		orgField.setText(attendeeView.attendee().org());
		orgField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));

		checkInButton = new JButton("Check In");
		checkInButton.setPreferredSize(new Dimension(200, 50));
		checkInButton.setFont(checkInButton.getFont().deriveFont(Font.BOLD));
		checkInButton.addActionListener(new CheckInListener());
	}
	
	private JPanel getPersonalInfoPanel()
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel title = new JLabel("Please enter personal information:");
				title.setFont(title.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(title, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				title = new JLabel("First Name:");
			layoutHelper.add(title, BorderLayout.EAST);
		panel.add(layoutHelper);
		panel.add(fNameField);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				title = new JLabel("Last Name:");
			layoutHelper.add(title, BorderLayout.EAST);
		panel.add(layoutHelper);
		panel.add(lNameField);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				title = new JLabel("Organization:");
			layoutHelper.add(title, BorderLayout.EAST);
		panel.add(layoutHelper);
		panel.add(orgField);
		
		panel.add(new JPanel());
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(3, 100, 10, 3));
			layoutHelper.add(saveButton, BorderLayout.CENTER);
		panel.add(layoutHelper);
		
		return panel;
	}
	
	private JPanel getCheckInPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(checkInButton, BorderLayout.EAST);
		return panel;
	}
	
	private class UpdateInfoListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			attendeeView().attendee().setFirstName(fNameField.getText());
			attendeeView().attendee().setLastName(lNameField.getText());
			attendeeView().attendee().setOrg(orgField.getText());
			attendeeView().attendee().saveData();
		}
	}
	
	private class CheckInListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			if(attendeeView().attendee().canCheckIn())
			{
				CMSCheckInDialog dialog = new CMSCheckInDialog(attendeeView);
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
			}
			else
			{
				JOptionPane.showMessageDialog(CMSRes.frame(),
					    "Check-in cannot proceed - user data has not been " +
					    "completed, or no events are being attended!",
					    "Check In Failed",
					    JOptionPane.WARNING_MESSAGE);
			}
		}
	}
}